package cn.iocoder.ny.module.mall.controller.admin.category;


import cn.iocoder.ny.module.mall.annotation.Encrypt;
import cn.iocoder.ny.module.mall.controller.admin.category.dto.MallProductByCategoryDTO;
import cn.iocoder.ny.module.mall.controller.admin.category.vo.MallProductByCategoryPageVo;
import cn.iocoder.ny.module.mall.controller.admin.category.vo.MallProductCategoryPageVo;
import cn.iocoder.ny.module.mall.domain.MallProductCategory;
import cn.iocoder.ny.module.mall.domain.dto.MallProductMenuDTO;
import cn.iocoder.ny.module.mall.service.IMallProductCateGoryService;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.security.PermitAll;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.error;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


/**
 * @author luchunhong
 * @date 2024/07/23/ 14:55
 * @description: 商品分类
 */
@RestController
@RequestMapping("/productCateGory")
public class MallProductCateGoryController {

    @Autowired
    private IMallProductCateGoryService mailProductCateGoryService;


//    @PostMapping("queryList")
//    public CommonResult<PageResult<MallBanner>>  queryBannerList(@RequestBody MallBannerPageVo mallBanner){
//        PageResult<MallBanner> mallBanners= iMallBannerService.getBannerPage(mallBanner);
//        return success(mallBanners);
//    }

    /*
    分类列表查询 分页
     */
    @PostMapping("queryList")
//    @Encrypt
    @PermitAll
    public  CommonResult<PageResult<MallProductCategory>> queryProductCateGoryList(@RequestBody MallProductCategoryPageVo mallProductCategory) {
        PageResult<MallProductCategory> mallProductCategories = mailProductCateGoryService.queryList(mallProductCategory);
        return success(mallProductCategories);
    }
    /*
     * 根据ID查询
     */
    @GetMapping("queryById/{id}")
//    @Encrypt
    @PermitAll
    public CommonResult queryById(@PathVariable Long id){
        return success(mailProductCateGoryService.queryById(id));
    }


    /*
    批量软删除
     */
    @PostMapping("updateBatch")
    @PreAuthorize("@ss.hasAnyRoles('merchant_admin', 'super_admin','mall_operator')")
    public CommonResult updateStatus(@RequestBody List<MallProductCategory> list){
//        if (!mailProductCateGoryService.updateBatch(list)){
//            return error(500,"请先删除该分类下的所有子菜单，再进行此操作。");
//        }
        list.forEach(e -> e.setDelFlag(1));
        return success(mailProductCateGoryService.updateBatch(list));
    }

    /*
    根据ID修改
     */
    @PostMapping("updateById")
    @PreAuthorize("@ss.hasAnyRoles('merchant_admin', 'super_admin','mall_operator')")
    public CommonResult updateById(@RequestBody @Validated(MallProductCategory.Edit.class) MallProductCategory mallProductCategory){

        boolean b = mailProductCateGoryService.updateProductCategoryById(mallProductCategory);
        if (!b){
            return error(500,"修改失败,请检查分类名称是否重复");
        }
        return success("修改成功");
    }

    /*
    新增分类
     */
    @PostMapping("add")
    @PreAuthorize("@ss.hasAnyRoles('merchant_admin', 'super_admin','mall_operator')")
    public CommonResult add(@RequestBody  @Validated(MallProductCategory.Add.class) MallProductCategory mallProductCategory){

        boolean b = mailProductCateGoryService.saveProductCategory(mallProductCategory);
        if (!b){
            return error(500,"新增失败,请检查分类名称是否重复");
        }
        return success("添加成功");
    }
    /*
    查询一级菜单列表
     */
    @PostMapping("queryFirstMenu")
    @PermitAll
    public CommonResult queryFirstMenu(@RequestBody MallProductCategory mallProductCategory){
        return success(mailProductCateGoryService.queryFirstMenu(mallProductCategory));
    }


    /*
    查询上级分类信息
     */
    @GetMapping("queryCategoryParent/{parentId}")
    @PermitAll
    public CommonResult queryCategoryParent(@PathVariable Long parentId){
        return success(mailProductCateGoryService.queryCategoryParent(parentId));
    }


    /*
    分类菜单查询
    */
    @PostMapping("menuSearch")
//    @Encrypt
    @PermitAll
    public CommonResult menuSearch(@RequestBody MallProductMenuDTO mallProductMenuDTO){
        List<MallProductMenuDTO> mallProductMenuDTOS = mailProductCateGoryService.menuSearch(mallProductMenuDTO);
        return success(mallProductMenuDTOS);
    }
    /*
     * 根据分类ID查询商品
     */
    @PostMapping("queryProductListById")
//    @Encrypt
    @PermitAll
    @TenantIgnore
    public CommonResult queryProductListById(@RequestBody MallProductByCategoryPageVo mallProductByCategoryPageVo){
        PageResult<MallProductByCategoryDTO> mallProductByCategoryDTOPageResult = mailProductCateGoryService.queryProductListByCategoryId(mallProductByCategoryPageVo);
        return success(mallProductByCategoryDTOPageResult);
    }
}

